package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.POJO.Food;
import com.itheima.POJO.HealthScheme;
import com.itheima.POJO.Sport;
import com.itheima.POJO.Template;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @auther zhaorui
 * @date 2019-12-05 - 16:07
 */
public interface TemplateMapper {

    /**
     * 添加
     * @param template
     */
    @Insert("INSERT INTO t_template(title,keyword,crowd,target,status,remark) " +
            "VALUES (#{title},#{keyword},#{crowd},#{target},#{status},#{remark})")
    @SelectKey(keyProperty = "id",statement = "select last_insert_id();",resultType = int.class,before = false)
    public void add(Template template);


    /**
     * 根据条件查询数据
     * @param queryString
     * @return
     */
    @Select("<script> " +
                "select * from t_template" +
                "<if test = 'value != null and  value.length > 0'>" +
                     "where title like concat('%',#{value},'%') OR keyword like concat('%',#{value},'%')" +
                "</if>" +
            "</script>")
    Page<Template> findPageByQueryString(String queryString);

    /**
     * 修改状态  启用/禁用
     * @param id
     * @param status
     */
    @Update("update t_template set status = #{arg1} where id = #{arg0}")
    public void updateTemplateByStatus(Integer id, String status);

    /**
     * 查询所有t_healthscheme
     * @return
     */
    @Select("select  * from t_healthscheme")
    public List<HealthScheme> findAllHealth();

    /**
     * 查询所有sport项
     * @return
     */
    @Select("select  * from t_sport")
    public List<Sport> findAllSport();

    /**
     * 查询所有food项
     * @return
     */
    @Select("select  * from t_food")
    public List<Food> findAllFood();

    /**
     * 添加表关联
     * @param map
     */
    @Insert("insert into t_template_healthscheme  (template_id,healthscheme_id)\n" +
            "        values(#{templateId},#{healthId}) ")
    public void setTemplateAndHealthScheme(Map<String, Integer> map);

    /**
     * 添加templat和food表的关联
     * @param map
     */
    @Insert("insert into t_template_food  (template_id,food_id)\n" +
            "        values(#{templateId},#{foodId}) ")
    public void setTemplateAndFood(Map<String, Integer> map);

    /**
     * 添加template和sport表的关联
     * @param map
     */
    @Insert("insert into t_template_sport  (template_id,sport_id)\n" +
            "        values(#{templateId},#{sportId}) ")
    public void setTemplateAndSport(Map<String, Integer> map);

    /**
     * 根据id查询template对象
     * @param id
     * @return
     */
    @Select("select * from t_template where id = #{id}")
    public Template findTemplateById(Integer id);

    @Select("select sport_id from t_template_sport  where template_id = #{id}")
    public List<Integer> findTemplateAndSportByTemplateId(Integer id);

    @Select("select food_id from t_template_food  where template_id = #{id}")
    public List<Integer> findTemplateAndFoodByTemplateId(Integer id);

    @Select("select healthscheme_id from t_template_healthscheme  where template_id = #{id}")
    public List<Integer> findTemplateAndHealthByTemplateId(Integer id);


    /**
     * 编辑功能
     * @param template
     */
    @Update("<script>" +
            " update t_template" +
            "        <set>" +
            "            <if test='title != null'>  title = #{title},</if>" +
            "            <if test='keyword != null'> keyword = #{keyword},</if>" +
            "            <if test='crowd != null'>  crowd = #{crowd},</if>" +
            "            <if test='target != null'> target = #{target},</if>" +
            "            <if test='status != null'> status = #{status},</if>" +
            "            <if test='remark != null'> remark = #{remark},</if>" +
            "        </set>" +
            "        where id = #{id}"+
            "</script>")
    public void edit(Template template);

    @Delete("delete from t_template_healthscheme where template_id = #{id}")
    public void deleteAssociation4Health(Integer templateId);

    @Delete("delete from t_template_food where template_id = #{id}")
    public void deleteAssociation4Food(Integer templateId);

    @Delete("delete from t_template_sport where template_id = #{id}")
    public void deleteAssociation4Sport(Integer templateId);


    /**
     * 查询当前id在关联表中的数量
     * @param id
     * @return
     */
    @Select("select count(template_id) from t_template_healthscheme where template_id = #{id}")
    public long findAssociation4Health(Integer id);

    @Select("select count(template_id) from t_template_food where template_id = #{id}")
    public long findAssociation4Food(Integer id);

    @Select("select count(template_id) from t_template_sport where template_id = #{id}")
    public long findAssociation4Sport(Integer id);

    /**
     * 删除
     * @param id
     */
    @Delete("delete from t_template where id = #{id}")
    public void deleteByTemplateId(Integer id);
}
